import cv2
import numpy as np
from memory_profiler import profile


# profile(precision=4,stream=open('memory_profiler.log','w+'))
@profile(precision=4)
def canny_detect(image_path):
    # 读取图像
    image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

    # 检查图片是否正确加载
    if image is None:
        print("无法加载图片")
        return None

    # 创建一个彩色图像以便显示红色边界
    color_image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)

    # 找到灰色与白色之间的边界
    # 假设灰色为128，白色为255
    edges = cv2.Canny(image, 127, 255)

    # 将边界标记为红色
    color_image[edges > 0] = [0, 0, 255]

    # 显示结果
    cv2.imshow('Frontiers', color_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    return color_image


if __name__ == '__main__':
    # 使用该函数
    canny_detect('../img/internet.jpg')
